package dch.persistent.page.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import dch.db.DataBaseDao;
import dch.db.DataBaseImpl;
import dch.entity.Types;
import dch.persistent.page.dao.TypesDao;

public class TypesDaoImpl implements TypesDao {

	private String sql=null;
	private Connection conn=null;
	private PreparedStatement pstmt=null;
	private ResultSet rs=null;
	private Types type=null;
	private List<Types> types=null;
	private DataBaseDao db=new DataBaseImpl();
	
	public TypesDaoImpl(){
		conn=db.getConnection();
	}
	
	@Override
	public List<Types> getSubTypes(int father) {
		sql="select typename,typeid from types where father=? limit 0,6";
		types=new ArrayList<Types>();
		try {
			pstmt=conn.prepareStatement(sql);
			pstmt.setInt(1,father);
			rs=pstmt.executeQuery();
			while(rs.next()){
				type=new Types();
				type.setTypename(rs.getString("typename"));
				type.setTypeid(rs.getInt("typeid"));
				types.add(type);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return types;
	}

	@Override
	public void close() {
		try{
			if(null!=rs)
				rs.close();
			if(null!=pstmt)
				pstmt.close();
			if(null!=conn)
				conn.close();
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}
	

}
